package cn.liyiming.bbs.modular.system.service.impl;

import cn.liyiming.bbs.core.common.enums.LikedStatusEnum;
import cn.liyiming.bbs.modular.system.model.Like;
import cn.liyiming.bbs.modular.system.model.LikedCountDTO;
import cn.liyiming.bbs.modular.system.service.ILikeService;
import cn.liyiming.bbs.modular.system.service.IRedisService;
import cn.liyiming.bbs.util.RedisKeyUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.Cursor;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.ScanOptions;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * @program: bbsblog
 * @description: redis操作点赞功能
 * @author: 李一鸣
 * @create: 2020-04-05 16:43
 **/
@Service
public class RedisServiceImpl implements IRedisService {
//    @Autowired
//    private RedisTemplate redisTemplate;
    @Resource(name = "intRedisTemplate")
    private RedisTemplate<String, Integer> redisTemplate;
    @Autowired
    private ILikeService likeService;


    @Override
    public void saveLiked2Redis(Integer likedArticleId, Integer likedUserId) {
        String key = RedisKeyUtils.getLikedKey(likedArticleId, likedUserId);
        redisTemplate.opsForHash().put(RedisKeyUtils.MAP_KEY_USER_LIKED, key, LikedStatusEnum.LIKE.getCode());
    }

    @Override
    public void unlikeFromRedis(Integer likedArticleId, Integer likedUserId) {
        String key = RedisKeyUtils.getLikedKey(likedArticleId, likedUserId);
        redisTemplate.opsForHash().put(RedisKeyUtils.MAP_KEY_USER_LIKED, key, LikedStatusEnum.UNLIKE.getCode());
    }

    @Override
    public void deleteLikedFromRedis(Integer likedArticleId, Integer likedUserId) {
        String key = RedisKeyUtils.getLikedKey(likedArticleId, likedUserId);
        redisTemplate.opsForHash().delete(RedisKeyUtils.MAP_KEY_USER_LIKED, key);
    }

    @Override
    public void incrementLikedCount(Integer likedArticleId) {
        redisTemplate.opsForHash().increment(RedisKeyUtils.MAP_KEY_USER_LIKED_COUNT, likedArticleId+"", 1);
    }

    @Override
    public void decrementLikedCount(Integer likedArticleId) {
        redisTemplate.opsForHash().increment(RedisKeyUtils.MAP_KEY_USER_LIKED_COUNT, likedArticleId+"", -1);
    }

    @Override
    public List<Like> getLikedDataFromRedis() {
        Cursor<Map.Entry<Object, Object>> cursor = redisTemplate.opsForHash().scan(RedisKeyUtils.MAP_KEY_USER_LIKED, ScanOptions.NONE);
        List<Like> list = new ArrayList<>();
        while (cursor.hasNext()){
            Map.Entry<Object, Object> entry = cursor.next();
            String key = (String) entry.getKey();
            //分离出 likedArticleId，likedUserId
            String[] split = key.split("::");
            String likedArticleId = split[0];
            String likedUserId = split[1];
            Integer value = (Integer) entry.getValue();

            //组装成 Like 对象
            Like like = new Like();
            like.setArticleId(Integer.valueOf(likedArticleId));
            like.setLikeUserId(Integer.valueOf(likedUserId));
            like.setLikeStatus(value);
            list.add(like);
            //redisTemplate.opsForHash().delete(RedisKeyUtils.MAP_KEY_USER_LIKED, key);
            //存到 list 后从 Redis 中删除
        }

        return list;
    }

    @Override
    public List<LikedCountDTO> getLikedCountFromRedis() {
        Cursor<Map.Entry<Object, Object>> cursor = redisTemplate.opsForHash().scan(RedisKeyUtils.MAP_KEY_USER_LIKED_COUNT, ScanOptions.NONE);
        List<LikedCountDTO> list = new ArrayList<>();
        while (cursor.hasNext()){
            Map.Entry<Object, Object> map = cursor.next();
            //将点赞数量存储在 LikedCountDT
            String key = (String)map.getKey();
            Integer keyy = Integer.valueOf(key);
            System.out.println("map.key:_______________________ "+map.getKey());
            System.out.println("map.value:_______________________ "+map.getValue());
            LikedCountDTO dto = new LikedCountDTO(keyy, (Integer) map.getValue());
            list.add(dto);
            //从Redis中删除这条记录
            //redisTemplate.opsForHash().delete(RedisKeyUtils.MAP_KEY_USER_LIKED_COUNT, key);
        }
        return list;
    }

}
